//
//  XTXHearCell.m
//  xiaotianxin
//
//  Created by XYC on 2019/10/25.
//  Copyright © 2019 XYC. All rights reserved.
//

#import "XTXHearCell.h"
#import "STKAudioPlayer.h"
#import "XTXVoiceModel.h"
@interface XTXHearCell ()<STKAudioPlayerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *backgImageView;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UIButton *playVoiceBtn;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIButton *moreButton;
@property (weak, nonatomic) IBOutlet UILabel *nichengLabel;
@property (nonatomic, weak) STKAudioPlayer *player;
@property (nonatomic, strong) NSArray *imagesArr;
@property (nonatomic, strong) NSString *idStr;
@property (nonatomic, strong) NSString *voiceStr;

@end

@implementation XTXHearCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.headImageView.layer.borderWidth = 2.5;
    self.headImageView.layer.borderColor = UIColor.whiteColor.CGColor;
    //复位
    _headImageView.image=[UIImage imageNamed:@"声音默认状态"];
    _playVoiceBtn.selected=NO;
    
}

-(void)setVoiceModel:(XTXVoiceModel *)voiceModel{
    _voiceModel=voiceModel;
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:voiceModel.headimage]];
    _nichengLabel.text=voiceModel.nickname;
    _timeLabel.text=[NSString stringWithFormat:@"0'%@\"", voiceModel.second];
    _idStr=voiceModel.user_id;
    _voiceStr=voiceModel.url;
}
-(void)setUserModel:(XTXUserModel *)userModel{
    _userModel=userModel;
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:userModel.headimage]];
    _nichengLabel.text=userModel.nickname;
    _timeLabel.text=[NSString stringWithFormat:@"0'%@\"", userModel.voicetime];
    _idStr=userModel.id;
    _voiceStr=userModel.voice;
}

- (IBAction)moreBtnClick:(id)sender {
    ///结束播放
    [self playVoiceEndAnimation];
    if ([_delegate respondsToSelector:@selector(collectionViewCell:moreBtnTouchEventWithID:)]) {
        [_delegate collectionViewCell:self moreBtnTouchEventWithID:self.idStr];
    }
}
//播放音频
- (IBAction)playVoiceBtnClick:(UIButton *)sender {
    if (sender.isSelected) {
        ///结束播放
        [self playVoiceEndAnimation];
    }else{
        if ([_delegate respondsToSelector:@selector(voiceWillBeginplay:)]) {
            [_delegate voiceWillBeginplay:self];
        }
        sender.selected=!sender.isSelected;
        if (sender.isSelected) {
            STKAudioPlayer *player = [[STKAudioPlayer alloc] init];
            [player playURL:[NSURL URLWithString:self.voiceStr]];
            player.delegate=self;
            _player=player;
            //开始执行动画
            [self playVocieAnimation];
            if ([_delegate respondsToSelector:@selector(collectionViewCell:playVoiceBtnTouchEvent:)]) {
                [_delegate collectionViewCell:self playVoiceBtnTouchEvent:sender];
            }
        }
    }
    
}
-(void)playVocieAnimation{
    //加载所有动画图片
    self.imagesArr=[self loadAllImagesWithimage];
    [self playVoiceAnimationWithImage:self.imagesArr count:MAXFLOAT duration:10];
}

/// 加载所有动画图片
- (NSArray *)loadAllImagesWithimage{
    NSMutableArray<UIImage *> *images = [NSMutableArray array];
    for (int i=0; i<7; i++) {
        // 获取所有图片的名称
        NSString *imageName = [NSString stringWithFormat:@"声音播放%d", i+1];
        UIImage *image = [UIImage imageNamed:imageName];
        // 装入数组
        [images addObject:image];
    }
    return images;
}
- (void)playVoiceAnimationWithImage:(NSArray *)images count: (NSInteger)count duration:(NSTimeInterval)duration{
    // 1.设置动画图片
    self.backgImageView.animationImages = images;
    // 2.设置动画次数
    self.backgImageView.animationRepeatCount = count;
    // 3.设置播放时长
    self.backgImageView.animationDuration = duration;
    // 4.播放
    [self.backgImageView startAnimating];
}
///结束播放
-(void)playVoiceEndAnimation{
    //结束播放
    _playVoiceBtn.selected=NO;
    [_player pause];
    //结束动画
    [_backgImageView stopAnimating];
    _backgImageView.image=[UIImage imageNamed:@"声音默认状态"];
    if ([_delegate respondsToSelector:@selector(playEndVoiceEvent:)]) {
        [_delegate playEndVoiceEvent:self];
    }
}
#pragma mark - STKAudioPlayerDelegate
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didFinishPlayingQueueItemId:(NSObject*)queueItemId withReason:(STKAudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration{
    [self playVoiceEndAnimation];
}
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer stateChanged:(STKAudioPlayerState)state previousState:(STKAudioPlayerState)previousState{
}
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didStartPlayingQueueItemId:(NSObject*)queueItemId{
}
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didFinishBufferingSourceWithQueueItemId:(NSObject*)queueItemId{
}
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer unexpectedError:(STKAudioPlayerErrorCode)errorCode{
}

@end
